/**
 * 获取指定名称的cookie值
 * @param {string} name - cookie的名称
 * @returns {string|undefined} - 返回cookie的值，如果cookie不存在则返回undefined
 */
function getCookie(name) {
  let matches = document.cookie.match(new RegExp(
    "(?:^|; )" + name.replace(/([.$?*|{}()[\]\\/+^])/g, '\\$1') + "=([^;]*)"
  ));
  return matches ? decodeURIComponent(matches[1]) : undefined;
}

/**
 * 设置Cookie函数
 * @param {string} name - Cookie的名称
 * @param {string} value - Cookie的值
 * @param {Object} [options={}] - Cookie的可选参数（如：path, expires 等）
 */
function setCookie(name, value, options = {}) {

  options = {
    path: '/',
    ...options
  };

  if (options.expires instanceof Date) {
    options.expires = options.expires.toUTCString();
  }

  let updatedCookie = encodeURIComponent(name) + "=" + encodeURIComponent(value);

  for (let optionKey in options) {
    updatedCookie += "; " + optionKey;
    let optionValue = options[optionKey];
    if (optionValue !== true) {
      updatedCookie += "=" + optionValue;
    }
  }

  document.cookie = updatedCookie;
}


/**
 * 删除指定名称的cookie
 * @param {string} name - 要删除的cookie的名称
 */
function deleteCookie(name) {
  setCookie(name, "", {
    'max-age': -1
  })
}